#!/bin/bash

if [[ $# -ne 2 ]]; then
    echo "The format is as follows:"
    echo "bash bisheng-jdk-8u322.sh buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    exit 1
fi

[[ ! "$1" =~ ^/.* || ! -d "$1" || "$1" = "/" ]] &&  echo "请输入正确的构建路径" && exit 1
[[ ! "$2" =~ ^/.* || ! -d "$2" || "$2" = "/" ]] &&  echo "请输入正确的安装路径" && exit 1

buildpath=$1
installpath=$2
[[ "$buildpath" =~ .*/$ ]] && buildpath=${buildpath%/*}
[[ "$installpath" =~ .*/$ ]] && installpath=${installpath%/*}
[[ "$buildpath" == "$installpath" ]] && echo "构建路径与安装路径需为不同路径" && exit 1
set -e
sudo yum install -y wget tar environment-modules libatomic 
source /etc/profile

  echo "# bisheng-jdk-8u322 正在下载"
  wget -t 10 -T 20 -c -P $buildpath https://mirrors.huaweicloud.com/kunpeng/archive/compiler/bisheng_jdk/bisheng-jdk-8u322-linux-aarch64.tar.gz
  if [[ $? -eq 0 ]]; then
    echo -e "\033[1;32;1m# bisheng-jdk-8u322 已经下载成功\033[0m"
  else
    echo -e "\033[31m# bisheng-jdk-8u322 下载失败,请重新执行脚本\033[0m"
    exit 1
  fi

  echo "# bisheng-jdk-8u322 正在安装"
  tar -xzf $buildpath/bisheng-jdk-8u322-linux-aarch64.tar.gz -C $buildpath  && rm -rf $buildpath/bisheng-jdk-8u322-linux-aarch64.tar.gz
  mkdir -p $installpath/bisheng-jdk-8u322 && cp -r $buildpath/bisheng-jdk1.8.0_322/* $installpath/bisheng-jdk-8u322/
  echo -e "\033[1;32;1m# bisheng-jdk-8u322 已经安装完毕\033[0m"

  cat >"$installpath/bisheng-jdk-8u322/jdk_modulefiles" <<EOF
#%Module1.0
conflict jdk
variable modfile [file normalize [info script]]
proc getModulefileDir {} {
  variable modfile
  set modfile_path [file dirname \$modfile]
  return \$modfile_path
}
set pwd [getModulefileDir]
set JAVA_HOME \$pwd
setenv JAVA_HOME \$JAVA_HOME
prepend-path PATH \$JAVA_HOME/bin
EOF

  rm -rf $buildpath/bisheng-jdk-8u322
